import 'package:hive/hive.dart';

import '../models/user_storm_info.model.dart';

class UserStore {
  static late Box _box;

  // 打开 Hive Box并初始化用户信息
  static Future<void> init() async {
    _box = await Hive.openBox('userBox');
    if (!_box.containsKey('UserInfo')) await _box.put('UserInfo', { 'token': '', 'phone': '' });
  }

  // 保存用户信息
  static Future<void> saveUserInfo(UserInfoModel userInfo) async {
    final data = userInfo.toMap();
    Map<String, dynamic> userinfo = Map<String, dynamic>.fromEntries(data.entries.map((entry) => MapEntry(entry.key.toString(), entry.value)));
    await _box.put('UserInfo', userinfo);
  }

  // 获取用户信息
  static UserInfoModel getUserInfo() => UserInfoModel.fromMap(_box.get('UserInfo', defaultValue: { 'token': '', 'phone': '' }));

  static Future<void> clearUserInfo() => _box.delete('UserInfo');
}
